package cn.edu.gcu.giftems.daos;

import cn.edu.gcu.giftems.pojos.Gift;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import java.util.List;

/**
 * 礼品Dao类.
 *
 * @author ZhengHQ
 * @create 2018-09-24
 * @see
 */
public class GiftDao extends HibernateDaoSupport {
    
    //获取礼品总记录
    public int findCount() {
        String hql = "select count(*) from Gift";
        //List list=new ArrayList();
        List<Long> list = (List<Long>)getHibernateTemplate().find(hql);
        if(list.size() > 0){
            return list.get(0).intValue();
        }
        return 0;
    }
    //获取礼品集合信息
    public List<Gift> findByPage(int begin, int pageSize) {
        try{
            String hql =  "select new Gift(g.id,g.name,g.pic,g.price,g.number,g.status,t.id,t.name )" +
                    "from Gift g, Type t where g.type = t.id order by g.id";
            List<Gift> list = (List<Gift>)this.getHibernateTemplate().execute(
                    new HibernateCallback<Object>() {
                        public Object doInHibernate(Session arg0)
                            throws HibernateException {
                            Query query = arg0.createQuery(hql);
                            query.setFirstResult(begin);
                            query.setMaxResults(pageSize);
                            return query.list();
                        }
                    }
            );
            return list;
        }catch (RuntimeException re) {
            throw re;
        }


    }
    //添加礼品
    public void save(Gift gift) {
        this.getHibernateTemplate().save(gift);
    }
    
    //根据礼品编号查询礼品信息
    public Gift finById(int id) {
        return this.getHibernateTemplate().get(Gift.class,id);
    }
    
    //更新礼品信息
    public void update(Gift gift) {
        this.getHibernateTemplate().update(gift);
    }
    
    //根据礼品id删除礼品
    public void delete(Gift gift) {
        this.getHibernateTemplate().delete(gift);
    }

    public List<Gift> findAll() {
        String hql = "from Gift";
        return (List<Gift>)this.getHibernateTemplate().find(hql);
    }

    public List<Gift> findByParameter(String filter_name, String simple_filter_value) {
        List<Gift> list = null;
        String hql = "select new Gift(g.id,g.name,g.pic,g.price,g.number,g.status,t.id,t.name )" +
                "from Gift g, Type t where g." + filter_name + " like '%" + simple_filter_value + "%'" +
                " and g.type = t.id order by g.id";
        try {
            list = (List<Gift>) this.getHibernateTemplate().execute(
                    new HibernateCallback<Object>() {
                        public Object doInHibernate(Session arg0)
                                throws HibernateException {
                            Query query = arg0.createQuery(hql);
                            return query.list();
                        }
                    }
            );
        } catch (RuntimeException re) {
            throw re;
        }
        return list;
    }
}
